package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.uicompent.InputPanel;
import kr.hkit.summary_java.dao.EmployeeDAO;
import kr.hkit.summary_java.erp.dto.Employee;

public class PanelEmployeeUI extends JPanel implements ActionListener,FocusListener {
	private Connection con;
	private EmployeeDAO dao;
	private JPanel panel_Center;
	private InputPanel panel_empNo;
	private InputPanel panel_empName;
	private InputPanel panel_title;
	private InputPanel panel_manager;
	private InputPanel panel_salary;
	private InputPanel panel_deptNo;
	private JPanel panel_Bottom;
	private JButton btn;
	private JButton btnClose;
	
	public PanelEmployeeUI() {
		con=ConnectionFactory.getConnection();
		initialize();
	}
	//버튼하나로 여러버튼 생성하기
	public String getBtnNewButton() {
		return btn.getText();
	}
	public void setBtnNewButton(String text) {
		btn.setText(text);
	}

	private void initialize() {
		
		setLayout(new BorderLayout(0, 0));
		
		panel_Center = new JPanel();
		add(panel_Center, BorderLayout.CENTER);
		panel_Center.setLayout(new GridLayout(0, 1, 0, 0));
		
		panel_empNo = new InputPanel("사원번호");
		panel_empNo.getTf().addFocusListener(this);
		GridLayout gridLayout = (GridLayout) panel_empNo.getLayout();
		panel_Center.add(panel_empNo);
		
		panel_empName = new InputPanel("사원이름");
		panel_Center.add(panel_empName);
		
		panel_title = new InputPanel("직책");
		panel_Center.add(panel_title);
		
		panel_manager = new InputPanel("매니저");
		panel_Center.add(panel_manager);
		
		panel_salary = new InputPanel("급여");
		panel_Center.add(panel_salary);
		
		panel_deptNo = new InputPanel("부서번호");
		panel_Center.add(panel_deptNo);
		
		panel_Bottom = new JPanel();
		add(panel_Bottom, BorderLayout.SOUTH);
		
		btn = new JButton("");
		if(btn.equals("검색")){
			panelDisable();
		}
		btn.addActionListener(this);
		btn.addActionListener(this);
		panel_Bottom.add(btn);
		
		btnClose = new JButton("닫기");
		btnClose.addActionListener(this);
		panel_Bottom.add(btnClose);
	}
	private void panelDisable() {
		// TODO Auto-generated method stub
		
	}
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")){
			actionPerformedBtnAdd(e);}
		if (e.getActionCommand().equals("수정")){
			actionPerformedBtnUpdate(e);}
		if (e.getActionCommand().equals("삭제")){
			actionPerformedBtnDelete(e);}
		if (e.getActionCommand().equals("검색")){
			actionPerformedBtnSelect(e);}
		if (e.getActionCommand().equals("닫기")){
			actionPerformedBtnClose(e);}
			
			
			
		}
	//추가
	private void actionPerformedBtnAdd(ActionEvent e) {
		dao= new EmployeeDAO(con);
		int dno=Integer.parseInt(panel_deptNo.getTf().getText().trim());
		String ename=panel_empName.getTf().getText();
		int etitle=Integer.parseInt(panel_title.getTf().getText());
		int emanager=Integer.parseInt(panel_manager.getTf().getText());
		int esalary=Integer.parseInt(panel_salary.getTf().getText());
		int eno=Integer.parseInt(panel_empNo.getTf().getText());
		Employee emp= new Employee(eno, ename, etitle, emanager, esalary, dno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao.insertItem(emp);
			JOptionPane.showMessageDialog(this,"입력되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"입력실패 다시 확인하세요.");
		}
	}
	//수정
	private void actionPerformedBtnUpdate(ActionEvent e) {
		dao= new EmployeeDAO(con);
		int dno=Integer.parseInt(panel_deptNo.getTf().getText().trim());
		String ename=panel_empName.getTf().getText();
		int etitle=Integer.parseInt(panel_title.getTf().getText());
		int emanager=Integer.parseInt(panel_manager.getTf().getText());
		int esalary=Integer.parseInt(panel_salary.getTf().getText());
		int eno=Integer.parseInt(panel_empNo.getTf().getText());
		Employee emp= new Employee(eno, ename, etitle, emanager, esalary, dno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao.updateItemByNo(emp);
			JOptionPane.showMessageDialog(this,"수정되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"수정실패 다시 확인하세요.");
		}
	}
	
	private void actionPerformedBtnDelete(ActionEvent e) {
		dao= new EmployeeDAO(con);
		int dno=Integer.parseInt(panel_deptNo.getTf().getText().trim());
		String ename=panel_empName.getTf().getText();
		int etitle=Integer.parseInt(panel_title.getTf().getText());
		int emanager=Integer.parseInt(panel_manager.getTf().getText());
		int esalary=Integer.parseInt(panel_salary.getTf().getText());
		int eno=Integer.parseInt(panel_empNo.getTf().getText());
		Employee emp= new Employee(eno, ename, etitle, emanager, esalary, dno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao.deleteItemByNo(emp);
			JOptionPane.showMessageDialog(this,"삭제되었습니다");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,"삭제실패 다시 확인하세요.");
		}
	}
	
	
	//닫기 
	private void actionPerformedBtnClose(ActionEvent e) {
		panel_deptNo.setVisible(false);
		panel_empName.setVisible(false);
		panel_empNo.setVisible(false);
		panel_manager.setVisible(false);
		panel_salary.setVisible(false);
		panel_title.setVisible(false);
		btn.setVisible(false);
		btnClose.setVisible(false);
		
	}
	//검색
	private void actionPerformedBtnSelect(ActionEvent e) {
		searchItem();
	}
	private void searchItem() {
		Connection con= ConnectionFactory.getConnection();
		dao = new EmployeeDAO(con);
		
		int eno=Integer.parseInt(panel_empNo.getTf().getText());
		Employee emp= new Employee();
		emp.setEmpNo(eno);
		try{
			emp=dao.searchItem(emp);
			panel_empName.setTf(emp.getEmpName());
			panel_title.setTf(emp.getTitle()+"");
			panel_manager.setTf(emp.getTitle()+"");
			panel_salary.setTf(emp.getSalary()+"");
			panel_deptNo.setTf(emp.getDeptNo()+"");
		}catch(SQLException e){
			e.printStackTrace();
		}catch(NullPointerException e){
			if(!btn.getText().equals("추가")){
				JOptionPane.showMessageDialog(PanelEmployeeUI.this, "해당부서가 없어요");
			}else{
				JOptionPane.showMessageDialog(PanelEmployeeUI.this, "추가가능");
			}
		}
		
	}
	//사원번호 입력후 tab키 누르면 자동생성(값)
	@Override
	public void focusGained(FocusEvent e) {
		
		
	}
	@Override
	public void focusLost(FocusEvent e) {
		
		
	}
	}
	


